package backend.component;

import java.util.List;

public class RISCVglobalother extends RISCVglobalvar{
    public RISCVglobalother(String name , boolean isinit, int size, List<Integer> elements) {
        setName(name);
        this.isInit = isinit;
        if(this.isInit){
            this.size = 4 * elements.size();
            this.elements = elements;
        }
        else{
            this.size = size;
            this.elements = null;
        }
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(GLOBAL).append(name).append("\n");
        if(isInit){
            s.append(SECTION_DATA);
        }
        else {
            s.append(SECTION_BSS);
        }
        s.append(String.format(TYPE,name,"@object"));
        s.append(String.format(SIZE,name,size));
        s.append(name).append(":\n");

        if(isInit){
            for(int value:elements){
                s.append(String.format(String.format(WORD,value)));
            }
            if(elements.size() * 4 < size){
                int last_size = size - elements.size() * 4;
                s.append(String.format(ZERO, last_size));
            }
        }
        else {
            s.append(String.format(ZERO, size));
        }
        return s.toString();
    }
}
